<?php

namespace app\api\controller;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 用户控制器
 */
use think\Db;
use think\Log;
class Member extends MobileMember {

    public function _initialize() {
        parent::_initialize();
    }

    /**
     * @api {POST} api/Member/index 用户首页基本信息显示
     * @apiVersion 1.0.0
     * @apiGroup Member
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.member_info  会员信息
     * @apiSuccess {String} result.member_info.available_predeposit  可用预存款余额
     * @apiSuccess {String} result.member_info.available_rc_balance  可用充值卡余额
     * @apiSuccess {String} result.member_info.exppoints  当前等级所需经验值
     * @apiSuccess {String} result.member_info.freeze_predeposit  冻结预存款余额
     * @apiSuccess {String} result.member_info.freeze_rc_balance  冻结充值卡余额
     * @apiSuccess {String} result.member_info.inform_allow  是否允许举报(1可以/2不可以)
     * @apiSuccess {String} result.member_info.inviter_id  推荐人ID
     * @apiSuccess {String} result.member_info.inviter_state  推广员状态（0审核中1已审核2已清退）
     * @apiSuccess {String} result.member_info.is_allowtalk  会员是否有咨询和发送站内信的权限 1为开启 0为关闭
     * @apiSuccess {String} result.member_info.is_buylimit  会员是否有购买权限 1为开启 0为关闭
     * @apiSuccess {String} result.member_info.level  会员等级
     * @apiSuccess {String} result.member_info.level_name  等级名称
     * @apiSuccess {String} result.member_info.member_addtime  添加时间，Unix时间戳
     * @apiSuccess {String} result.member_info.member_areaid  地区ID
     * @apiSuccess {String} result.member_info.member_areainfo  地区信息
     * @apiSuccess {String} result.member_info.member_avatar  用户头像
     * @apiSuccess {String} result.member_info.member_birthday  生日
     * @apiSuccess {String} result.member_info.member_cityid  城市ID
     * @apiSuccess {String} result.member_info.member_email  邮箱
     * @apiSuccess {String} result.member_info.member_emailbind  已绑定邮箱 0否1是
     * @apiSuccess {String} result.member_info.member_exppoints  会员经验值
     * @apiSuccess {String} result.member_info.member_id  用户ID
     * @apiSuccess {String} result.member_info.member_login_ip  本次登录IP
     * @apiSuccess {String} result.member_info.member_loginnum  本次登录次数
     * @apiSuccess {String} result.member_info.member_logintime  本次登录时间，Unix时间戳
     * @apiSuccess {String} result.member_info.member_mobile  手机号
     * @apiSuccess {String} result.member_info.member_mobilebind  已绑定手机 0否1是
     * @apiSuccess {String} result.member_info.member_name  用户名称
     * @apiSuccess {String} result.member_info.member_old_login_ip  上次登录IP
     * @apiSuccess {String} result.member_info.member_old_logintime  上次登录时间，Unix时间戳
     * @apiSuccess {String} result.member_info.member_points  用户积分
     * @apiSuccess {String} result.member_info.member_privacy  隐私设定
     * @apiSuccess {String} result.member_info.member_provinceid  省份ID
     * @apiSuccess {String} result.member_info.member_qq  用户QQ
     * @apiSuccess {String} result.member_info.member_qqinfo  qq快捷登录信息
     * @apiSuccess {String} result.member_info.member_qqopenid  qq openid
     * @apiSuccess {String} result.member_info.member_sex  会员性别 0保密1男2女3保密
     * @apiSuccess {String} result.member_info.member_sinainfo  新浪快捷登录信息
     * @apiSuccess {String} result.member_info.member_sinaopenid  新浪openid
     * @apiSuccess {String} result.member_info.member_state  会员的开启状态 1为开启 0为关闭
     * @apiSuccess {String} result.member_info.member_truename  会员真实姓名
     * @apiSuccess {String} result.member_info.member_ww  用户旺旺
     * @apiSuccess {String} result.member_info.member_wxinfo  微信快捷登录信息
     * @apiSuccess {String} result.member_info.member_wxopenid  微信openid
     * @apiSuccess {String} result.member_info.member_wxunionid  微信unionid
     * @apiSuccess {String} result.member_info.order_noeval_count  待评论订单数
     * @apiSuccess {String} result.member_info.order_nopay_count  待付款订单数
     * @apiSuccess {String} result.member_info.order_noreceipt_count  待收货订单数
     * @apiSuccess {String} result.member_info.order_noship_count  待发货订单数
     * @apiSuccess {String} result.member_info.order_refund_count  退款中订单数
     * @apiSuccess {String} result.member_info.store_id  店铺ID
     * @apiSuccess {String} result.member_info.voucher_count  可用优惠券数
     * @apiSuccess {String} result.member_info.member_signin_time  最后一次签到时间
     * @apiSuccess {String} result.member_info.member_signin_days_cycle  持续签到天数，每周期后清零
     * @apiSuccess {String} result.member_info.member_signin_days_total  签到总天数
     * @apiSuccess {String} result.member_info.member_signin_days_series  持续签到天数总数，非连续周期清零
     */
    public function index() {
        $member_model = model('member');
        $member_info = $member_model->getMemberInfoByID($this->member_info['member_id']);
        
        unset($member_info['member_password']);
        unset($member_info['member_paypwd']);
        
        if ($member_info) {
            $member_gradeinfo = $member_model->getOneMemberGrade(intval($member_info['member_exppoints']));
            $member_info = array_merge($member_info, $member_gradeinfo);
            //代金券数量
            $member_info['voucher_count'] = model('voucher')->getCurrentAvailableVoucherCount($this->member_info['member_id']);
            $member_info['member_avatar'] = get_member_avatar_for_id($this->member_info['member_id']);
            $member_info['member_idcard_image1_url'] = get_member_idcard_image($member_info['member_idcard_image1']);
            $member_info['member_idcard_image2_url'] = get_member_idcard_image($member_info['member_idcard_image2']);
            $member_info['member_idcard_image3_url'] = get_member_idcard_image($member_info['member_idcard_image3']);
        }
        
        //获取用户是否有推广权限
        if (config('inviter_open')) {
            //查看是否已是分销会员
            $inviter_model = model('inviter');
            $inviter_info = $inviter_model->getInviterInfo('i.inviter_id=' . $this->member_info['member_id']);
            if(!empty($inviter_info)){
                $member_info['inviter_state'] = $inviter_info['inviter_state']; // 是否是分销员
            }
        }
        
        //判断此用户是否有店铺
        $seller_info = model('seller')->getSellerInfo(array('member_id' => $this->member_info['member_id']));
        if ($seller_info) {
            $member_info['store_id'] = $seller_info['store_id'];
        } else {
            $member_info['store_id'] = 0;
        }

        // 交易提醒
        $order_model = model('order');
        $refundreturn_model = model('refundreturn');
        $member_info['order_nopay_count'] = $order_model->getOrderCountByID('buyer', $this->member_info['member_id'], 'NewCount');
        $member_info['order_noreceipt_count'] = $order_model->getOrderCountByID('buyer', $this->member_info['member_id'], 'SendCount');
        $member_info['order_noeval_count'] = $order_model->getOrderCountByID('buyer', $this->member_info['member_id'], 'EvalCount');
        $member_info['order_noship_count'] = $order_model->getOrderCountByID('buyer', $this->member_info['member_id'], 'PayCount');
        $member_info['order_refund_count'] = $refundreturn_model->getRefundreturnCount(array('buyer_id' => $this->member_info['member_id'], 'refund_state' => array('<>', 3)));
        // 查询当前用户是否提交身份认证审核
        if($member_info['auth_type'] == 0){
            $member_info['auth_check_type'] = Db::table('dsc_user_auth')->where('user_id', $member_info['member_id'])->value('status') ?? 2;
        }elseif($member_info['auth_type'] == 1){
//            $member_info['auth_check_type'] = Db::table('dsc_job_apply')->where('uid', $member_info['member_id'])->value('status') ?? 2;
            $member_info['auth_check_type'] = Db::table('dsc_user_auth')->where('user_id', $member_info['member_id'])->value('status') ?? 2;
        }else{
            $member_info['auth_check_type'] = 2;
        }
	    $member_id = $this->member_info['member_id'];
	    $message_model = model('message');
	    $member_info['message_real'] = $message_model->getNewMessageCount($member_id);//获取未读消息
	    $member_info['site_tel']=\db('config')->where('code','site_tel400')->value('value');
        $member_info['iosShenHe'] = 0;
        ds_json_encode(10000, '', array('member_info' => $member_info));
    }

    public function my_asset() {
        $fields_arr = array('point', 'predepoit', 'available_rc_balance', 'voucher');
        $fields_str = trim(input('fields'));
        if ($fields_str) {
            $fields_arr = explode(',', $fields_str);
        }
        $member_info = array();
        if (in_array('point', $fields_arr)) {
            $member_info['point'] = $this->member_info['member_points'];
        }
        if (in_array('predepoit', $fields_arr)) {
            $member_info['predepoit'] = $this->member_info['available_predeposit'];
        }
        if (in_array('available_rc_balance', $fields_arr)) {
            $member_info['available_rc_balance'] = $this->member_info['available_rc_balance'];
        }
        if (in_array('voucher', $fields_arr)) {
            $member_info['voucher'] = model('voucher')->getCurrentAvailableVoucherCount($this->member_info['member_id']);
        }
        ds_json_encode(10000, '', $member_info);
    }


    /**
     * @api {POST} api/Member/information 用户基本信息显示
     * @apiVersion 1.0.0
     * @apiGroup Member
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object} result.member_info  会员信息
     * @apiSuccess {String} result.member_info.available_predeposit  可用预存款余额
     * @apiSuccess {String} result.member_info.available_rc_balance  可用充值卡余额
     * @apiSuccess {String} result.member_info.freeze_predeposit  冻结预存款余额
     * @apiSuccess {String} result.member_info.freeze_rc_balance  冻结充值卡余额
     * @apiSuccess {String} result.member_info.inform_allow  是否允许举报(1可以/2不可以)
     * @apiSuccess {String} result.member_info.inviter_id  推荐人ID
     * @apiSuccess {String} result.member_info.is_allowtalk  会员是否有咨询和发送站内信的权限 1为开启 0为关闭
     * @apiSuccess {String} result.member_info.is_buylimit  会员是否有购买权限 1为开启 0为关闭
     * @apiSuccess {String} result.member_info.member_addtime  添加时间，Unix时间戳
     * @apiSuccess {String} result.member_info.member_areaid  地区ID
     * @apiSuccess {String} result.member_info.member_areainfo  地区信息
     * @apiSuccess {String} result.member_info.member_avatar  用户头像
     * @apiSuccess {String} result.member_info.member_birthday  生日
     * @apiSuccess {String} result.member_info.member_cityid  城市ID
     * @apiSuccess {String} result.member_info.member_email  邮箱
     * @apiSuccess {String} result.member_info.member_emailbind  已绑定邮箱 0否1是
     * @apiSuccess {String} result.member_info.member_exppoints  会员经验值
     * @apiSuccess {String} result.member_info.member_id  用户ID
     * @apiSuccess {String} result.member_info.member_login_ip  本次登录IP
     * @apiSuccess {String} result.member_info.member_loginnum  登录次数
     * @apiSuccess {String} result.member_info.member_logintime  本次登录时间，Unix时间戳
     * @apiSuccess {String} result.member_info.member_mobile  手机号
     * @apiSuccess {String} result.member_info.member_mobilebind  已绑定手机 0否1是
     * @apiSuccess {String} result.member_info.member_name  用户名称
     * @apiSuccess {String} result.member_info.member_old_login_ip  上次登录IP
     * @apiSuccess {String} result.member_info.member_old_logintime  上次登录时间，Unix时间戳
     * @apiSuccess {String} result.member_info.member_points  用户积分
     * @apiSuccess {String} result.member_info.member_privacy  隐私设定
     * @apiSuccess {String} result.member_info.member_provinceid  省份ID
     * @apiSuccess {String} result.member_info.member_qq  用户QQ
     * @apiSuccess {String} result.member_info.member_qqinfo  qq快捷登录信息
     * @apiSuccess {String} result.member_info.member_qqopenid  qq openid
     * @apiSuccess {String} result.member_info.member_sex  会员性别 0保密1男2女3保密
     * @apiSuccess {String} result.member_info.member_sinainfo  新浪快捷登录信息
     * @apiSuccess {String} result.member_info.member_sinaopenid  新浪openid
     * @apiSuccess {String} result.member_info.member_state  会员的开启状态 1为开启 0为关闭
     * @apiSuccess {String} result.member_info.member_truename  会员真实姓名
     * @apiSuccess {String} result.member_info.member_ww  用户旺旺
     * @apiSuccess {String} result.member_info.member_wxinfo  微信快捷登录信息
     * @apiSuccess {String} result.member_info.member_wxopenid  微信openid
     * @apiSuccess {String} result.member_info.member_wxunionid  微信unionid
     */
    public function information() {
        $member_model = model('member');
        $condition['member_id'] = $this->member_info['member_id'];
        $member_info = $member_model->getMemberInfo($condition);
        $member_info['member_avatar'] = get_member_avatar_for_id($member_info['member_id']);
        $member_info['member_idcard_image1_url'] = get_member_idcard_image($member_info['member_idcard_image1']);
            $member_info['member_idcard_image2_url'] = get_member_idcard_image($member_info['member_idcard_image2']);
            $member_info['member_idcard_image3_url'] = get_member_idcard_image($member_info['member_idcard_image3']);
        unset($member_info['member_password']);
        unset($member_info['member_paypwd']);
        ds_json_encode(10000, '', array('member_info'=>$member_info));
    }

    /**
     * @api {POST} api/Member/edit_information 用户基本信息修改
     * @apiVersion 1.0.0
     * @apiGroup Member
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {String} member_qq 会员QQ
     * @apiParam {String} member_ww 会员旺旺
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     */
    public function edit_information() {
        $data = array(
            'member_nickname' => input('param.member_nickname'),
            'member_sex' => input('param.member_sex'),
            'member_qq' => input('param.member_qq'),
            'member_ww' => input('param.member_ww'),
            'member_birthday' => strtotime(input('param.member_birthday')),
        );

        $member_validate = validate('member');
        if (!$member_validate->scene('edit_information')->check($data)) {
            ds_json_encode(10001, $member_validate->getError());
        }

        $member_model = model('member');
        $condition['member_id'] = $this->member_info['member_id'];
        $result = $member_model->editMember($condition, $data);
        if ($result) {
            ds_json_encode(10000, lang('ds_common_op_succ'));
        } else {
            ds_json_encode(10001, lang('ds_common_op_fail'));
        }
    }

    /**
     * @api {POST} api/Member/edit_memberavatar 更新用户头像
     * @apiVersion 1.0.0
     * @apiGroup Member
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {File} file 用户头像
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {String} result  用户头像
     */
    public function edit_memberavatar() {
        /*echo"666";
        exit;*/
        $file = request()->file('memberavatar');
        $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_AVATAR . DS;
        $avatar_name = 'avatar_' . $this->member_info['member_id'] . '.jpg';
        $info = $file->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file, $avatar_name);
        /*Log::error("头像调试1".json_encode($info));*/
        
        //生成缩略图
        $image = \think\Image::open($upload_file . '/' . $avatar_name);
        $image->thumb(100, 100, \think\Image::THUMB_CENTER)->save($upload_file . '/' . $avatar_name);
        /*Log::error("头像调试2".json_encode($info));*/

        if ($info) {
        /*Log::error("头像调试3".json_encode($info));
        Log::error("头像调试3.1".get_member_avatar_for_id($this->member_info['member_id']));*/
            //exit;
            ds_json_encode(10000, '', get_member_avatar_for_id($this->member_info['member_id']));
        } else {
        /*Log::error("头像调试4".json_encode($info));
            exit;*/
            // 上传失败获取错误信息
            ds_json_encode(10001, $file->getError());
        }
    }


	/**
	 * @api {POST} api/Member/edit_resumeavatar 更新简历头像
	 * @apiVersion 1.0.0
	 * @apiGroup Member
	 *
	 * @apiHeader {String} X-DS-KEY 用户授权token
	 *
	 * @apiParam {File} file 用户头像
	 *
	 * @apiSuccess {String} code 返回码,10000为成功
	 * @apiSuccess {String} message  返回消息
	 * @apiSuccess {String} result  用户头像
	 */
	public function edit_resumeavatar() {
		$file = request()->file('resumeavatar');
		$upload_file = BASE_UPLOAD_PATH . DS . ATTACH_RESUME . DS;
		$avatar_name = 'resume_' . $this->member_info['member_id'] . '.jpg';
		$info = $file->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file, $avatar_name);
		//生成缩略图
		$image = \think\Image::open($upload_file . '/' . $avatar_name);
		$image->thumb(100, 100, \think\Image::THUMB_CENTER)->save($upload_file . '/' . $avatar_name);
		if ($info) {
			ds_json_encode(10000, '', get_member_avatar_for_id($this->member_info['member_id']));
		} else {
			// 上传失败获取错误信息
			ds_json_encode(10001, $file->getError());
		}
	}
    public function auth()
    {
        $member_model = model('member');

            $member_array = array();
            $member_array['member_auth_state'] = 1;
            $member_array['member_idcard'] = input('post.member_idcard');
            $member_array['member_truename'] = input('post.member_truename');
            $member_validate = validate('member');
                if (!$member_validate->scene('auth')->check($member_array)) {
                    ds_json_encode(10001,$member_validate->getError());
                }
            if(!$this->member_info['member_idcard_image1']){
              ds_json_encode(10001,lang('member_idcard_image1_require'));
            }    
            if(!$this->member_info['member_idcard_image2']){
              ds_json_encode(10001,lang('member_idcard_image2_require'));
            }  
            if(!$this->member_info['member_idcard_image3']){
              ds_json_encode(10001,lang('member_idcard_image3_require'));
            }  
            if(!input('post.if_confirm')){
                ds_json_encode(10000);
            }
            $update = $member_model->editMember(array('member_id' => $this->member_info['member_id'],'member_auth_state'=>array('in',array(0,2))), $member_array);

            $message = $update ? lang('ds_common_op_succ') : lang('ds_common_op_fail');
            
            if($update){
                ds_json_encode(10000,$message);
            }else{
                ds_json_encode(10001,$message);
            }
        

    }


	/**
	 * 绑定手机号
	 * @return mixed
	 */
	public function phonebind()
	{
		$member_model = model('member');
		//$url=is_http().$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		//$url=cookie("url_site");
		if($this->member_info['is_phone']){
			ds_json_encode(10001,"手机号已绑定");
		}
		if (request()->isPost()) {
			$member_array = array();
			$member_array['is_phone'] = 1;
			$phone = input('post.phone');
			$code = input('post.code');
			//$member_validate = validate('member');

			if(!$phone){
				ds_json_encode(10001,"手机号不能为空");
			}
			if(!$code){
				ds_json_encode(10001,"验证码不能为空");
			}

			$tongshang=new Tongshangyun();
			$res=$tongshang->bindPhone($this->member_info['member_id'],$phone,$code);
			if(!$res['state']){
				if($res['message'] != "手机已绑定"){
					ds_json_encode(10001,$res['message']);
				}
			}
			$update = $member_model->editMember(array('member_id' => $this->member_info['member_id'],'is_phone'=>0), $member_array);

			$message = $update ? lang('ds_common_op_succ') : lang('ds_common_op_fail');

			if($update){
				ds_json_encode(10000,$message);
			}else{
				ds_json_encode(10001,$message);
			}
		}

		$this->assign('member_info', $this->member_info);
		/* 设置买家当前菜单 */
		$this->setMemberCurMenu('member_auth');
		/* 设置买家当前栏目 */
		$this->setMemberCurItem('phonebind');
		return $this->fetch($this->template_dir.'phonebind');
	}

	/**
	 * 手机号验证码
	 */
	public function phone_code()
	{
		$phone=input('phone');
		if(!$phone){
			ds_json_encode(10001,"手机号不能为空");
		}

		if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
			ds_json_encode(10001,"手机号格式不正确");
		}
		$tongshang=new Tongshangyun();
		$res=$tongshang->sendVerificationCode($this->member_info['member_id'],$phone);
		if(!$res['state']){
			ds_json_encode(10001,$res['message']);
		}else{
			ds_json_encode(10000,$res['msg']);
		}
	}
    public function edit_auth() {
        $file_name = input('param.id');
            if (!empty($_FILES[$file_name]['name'])) {

                $res=ds_upload_pic(ATTACH_IDCARD_IMAGE,$file_name);
                if(!$res['code']){
                    ds_json_encode(10001,$res['msg']);
                }

                $member_array=array();
                $member_array[substr($file_name,0,20)] = $res['data']['file_name'];
                $member_model = model('member');
                if(!$member_model->editMember(array('member_id' => $this->member_info['member_id'],'member_auth_state'=>array('in',array(0,2))), $member_array)){
                    ds_json_encode(10001,lang('ds_common_op_fail'));
                }
                ds_json_encode(10000,'',array('file_name'=>$res['data']['file_name'],'file_path'=>get_member_idcard_image($res['data']['file_name'])));
            }
            ds_json_encode(10001,lang('param_error'));
    }
    public function drop_auth(){
        $file_name=input('param.file_name');

        @unlink(BASE_UPLOAD_PATH . DS . ATTACH_IDCARD_IMAGE . DS . $this->member_info[$file_name]);
        $member_array=array();
        $member_array[$file_name] = '';
        $member_model = model('member');
        if(!$member_model->editMember(array('member_id' => $this->member_info['member_id'],'member_auth_state'=>array('in',array(0,2))), $member_array)){
                    ds_json_encode(10001,lang('ds_common_op_fail'));
                }
        ds_json_encode(10000);        
    }


	/**
	 * 发送手机验证码
	 */
	public function phone_comp_code()
	{
		$phone=input('phone');
		if(!$phone){
			ds_json_encode(10001,"手机号不能为空");
		}

		if(!preg_match("/^1[3456789]\d{9}$/", $phone)){
			ds_json_encode(10001,"手机号格式不正确");
		}

		$store_model = model('store');
		$store_info=$store_model->where(['member_id'=>$this->member_info['member_id']])->order('store_id desc')->find();
		if(!$store_info){//$store_info['store_id']
			$this->error("该店铺不存在");
		}
		$tongshang=new Tongshangyun();
		$res=$tongshang->sendVerificationCode($store_info['store_id'],$phone,9,1);
		if(!$res['state']){
			ds_json_encode(10001,$res['message']);
		}else{
			ds_json_encode(10000,$res['msg']);
		}
	}

	/**
	 * 用户个人中心 产品列表
	 */
	public function goods_list() {
		$goods_model = model('goods');
		$search_model = model('search');

		//查询条件
		$condition = array();
		$cate_id = intval(input('param.cate_id'));
		$keyword = $default_classid = input('param.keyword');
		$b_id = intval(input('param.b_id'));

		//获得经过属性过滤的商品信息
		$this->_model_search = model('search');
		list($goods_param, $brand_array, $initial_array, $attr_array, $checked_brand, $checked_attr) = $this->_model_search->getAttribute(input('param.'), $default_classid);
		if (isset($goods_param['class']['depth'])) {
			$condition['goodscommon.gc_id_' . $goods_param['class']['depth']] = $goods_param['class']['gc_id'];
		}
		if (isset($goods_param['goodsid_array'])) {
			$condition['goods.goods_id'] = array('in', $goods_param['goodsid_array']);
		}
		if ($cate_id > 0) {
			$condition['goodscommon.gc_id'] = $cate_id;
		}

		if ($b_id > 0) {
			$condition['goodscommon.brand_id'] = $b_id;
		}

		$price_from = input('param.price_from');
		$price_to = input('param.price_to');
		$price_from = preg_match('/^[\d.]{1,20}$/', $price_from) ? $price_from : null;
		$price_to = preg_match('/^[\d.]{1,20}$/', $price_to) ? $price_to : null;

		//所需字段
		$fieldstr = "goods.goods_id,goodscommon.goods_commonid,goodscommon.store_id,goodscommon.goods_name,goodscommon.goods_advword,goodscommon.goods_price,goods.goods_promotion_price,goods.goods_promotion_type,goodscommon.goods_marketprice,goodscommon.goods_image,goods.goods_salenum,goods.evaluation_good_star,goods.evaluation_count";

		$fieldstr .= ',goodscommon.is_virtual,goodscommon.is_presell,goodscommon.is_goodsfcode,goods.is_have_gift,goodscommon.store_name,goodscommon.is_platform_store';

		//排序方式
		$order = $this->_goods_list_order(input('param.sort_key'), input('param.sort_order'));

		//全文搜索搜索参数
		$indexer_searcharr = input('param.');
		//搜索消费者保障服务
		$search_ci_arr = array();
		$ci = trim(input('param.ci'), '_');
		if ($ci && $ci != 0 && is_string($ci)) {
			//处理参数
			$search_ci = $ci;
			$search_ci_arr = explode('_', $search_ci);
			$indexer_searcharr['search_ci_arr'] = $search_ci_arr;
		}
		if (input('param.own_shop') == 1) {
			$indexer_searcharr['type'] = 1;
		}
		$indexer_searcharr['price_from'] = $price_from;
		$indexer_searcharr['price_to'] = $price_to;

		//优先从全文索引库里查找
		//        list($goods_list, $indexer_count) = $search_model->indexerSearch($indexer_searcharr, $this->pagesize);
		//        if (!is_null($goods_list)) {
		//            $goods_list = array_values($goods_list);
		//        } else {

		if ($price_from && $price_to) {
			$condition['goods.goods_promotion_price'] = array('between', "{$price_from},{$price_to}");
		} elseif ($price_from) {
			$condition['goods.goods_promotion_price'] = array('egt', $price_from);
		} elseif ($price_to) {
			$condition['goods.goods_promotion_price'] = array('elt', $price_to);
		}
		if (input('param.gift') == 1) {
			$condition['goods.is_have_gift'] = 1;
		}
		if (input('param.own_shop') == 1) {
			$condition['goodscommon.store_id'] = 1;
		}
		if (intval(input('param.area_id')) > 0) {
			$condition['goodscommon.areaid_1'] = intval(input('param.area_id'));
		}

		//抢购和限时折扣搜索
		$_tmp = array();
		if (input('param.groupbuy') == 1) {
			$_tmp[] = 1;
		}
		if (input('param.xianshi') == 1) {
			$_tmp[] = 2;
		}
		if ($_tmp) {
			$condition['goods.goods_promotion_type'] = array('in', $_tmp);
		}
		unset($_tmp);

		//虚拟商品
		if (input('param.virtual') == 1) {
			$condition['goodscommon.is_virtual'] = 1;
		}

		$store_model = model('store');
		$store_info=$store_model->where(['member_id'=>$this->member_info['member_id']])->order('store_id desc')->find();
		if(!$store_info){//$store_info['store_id']
			ds_json_encode(10001, '该会员并没有开通商户');
		}

		$condition['goods.store_id']=$store_info['store_id'];
		$goods_list = $goods_model->getGoodsUnionList($condition, $fieldstr, $order,'goodscommon.goods_commonid', $this->pagesize);
		//        }
		//处理商品列表(抢购、限时折扣、商品图片)
		//$goods_list = $this->_goods_list_extend($goods_list);
		$result = array_merge(array('goods_list' => $goods_list), mobile_page(is_object($goods_model->page_info)?$goods_model->page_info:''));
		ds_json_encode(10000, '',$result);
	}

	/**
	 * 商品列表排序方式
	 */
	private function _goods_list_order($sort_key, $sort_order) {
		$result = 'goodscommon.mall_goods_commend desc,goodscommon.mall_goods_sort asc';
		if (!empty($sort_key)) {

			$sequence = 'desc';
			if ($sort_order == 'asc') {
				$sequence = 'asc';
			}

			switch ($sort_key) {
				//销量
				case 'goods_salenum' :
					$result = 'goods.goods_salenum' . ' ' . $sequence;
					break;
				//浏览量
				case 'goods_click' :
					$result = 'goods.goods_click' . ' ' . $sequence;
					break;
				//价格
				case 'goods_price' :
					$result = 'goodscommon.goods_price' . ' ' . $sequence;
					break;
				//新品
				case 'goods_addtime' :
					$result = 'goodscommon.goods_addtime' . ' ' . $sequence;
					break;
			}
		}
		return $result;
	}
}

?>
